/*
 * @lc app=leetcode.cn id=14 lang=swift
 *
 * [14] 最长公共前缀
 */

// @lc code=start
class Solution {
    func longestCommonPrefix(_ strs: [String]) -> String {
        guard strs.isEmpty == false else {
            return ""
        }
        
        guard strs.count != 1 else {
            return strs[0]
        }
        
        let firstString = strs[0]
        var result = ""
        for (idx, c) in firstString.enumerated() {
            for string in strs.dropFirst(){
                if idx >= string.count {
                    return result
                }
                
                if c != string[String.Index(utf16Offset: idx, in: string)] {
                    return result
                }
            }
            result.append(c)
        }
        return result
    }
}
// @lc code=end

